package com.lms.nio.selector;

import java.io.ByteArrayOutputStream;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-05-08 07:31
 * @Description: ${Description}
 */
public class NIOClient01 {
    public static void main(String[] args) throws  Exception{
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        boolean connect = socketChannel.connect(new InetSocketAddress("localhost",6666));
        if (!connect){
            while (!socketChannel.finishConnect()){
                System.out.println("因为连接需要时间，客户端不会阻塞，可以做其他工作");
            }
        }
//        String str = "hello,NIO test by李孟帅";
        System.out.println(socketChannel.getLocalAddress()+" 准备发送数据:");
        Scanner scanner = new Scanner(System.in);
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        while (true){
            String nextLine = scanner.nextLine();
            byteBuffer.put(nextLine.getBytes());
            byteBuffer.flip();
            socketChannel.write(byteBuffer);
            byteBuffer.clear();
        }



    }
}
